﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Kristall.Screens;
using Kristall;
using Microsoft.Xna.Framework.Content;
using Kristall.Managers;

namespace Kristall
{
    public abstract class Tool
    {
        #region Properties And Fields
        public Texture2D Cursor;
        public Vector2 MousePositionWorld = Vector2.Zero;
        public MouseState LastMouseState;
        public MouseState CurrentMouseState;
        public LevelEditor LevelEditor;

        public bool IsMouseDown = false;

        public abstract Keys Hotkey
        {
            get;
        }

        public abstract string CursorTexture
        {
            get;
        }

        public virtual Vector2 CursorOffset
        {
            get
            {
                return Vector2.Zero;
            }
        }

        public event EventHandler OnMouseUp;
        public event EventHandler OnMouseDown;
        #endregion

        public Tool(LevelEditor editor)
        {
            this.LevelEditor = editor;
        }

        public virtual void Reset()
        {
            IsMouseDown = false;
        }

        public virtual void MouseDown(Vector2 mousePosition)
        {
            IsMouseDown = true;

            if (OnMouseDown != null)
            {
                OnMouseDown(this, null);
            }
        }

        public virtual void MouseUp(Vector2 mousePosition)
        {
            IsMouseDown = false;

            if (OnMouseUp != null)
            {
                OnMouseUp(this, null);
            }
        }

        public virtual void LoadContent(ContentManager content)
        {
            Cursor = content.Load<Texture2D>(@"Tools\" + CursorTexture);
        }

        public virtual void HandleInput(InputState input)
        {
            LastMouseState = CurrentMouseState;
            CurrentMouseState = Mouse.GetState();

            Point mouseScreenPoint = new Point((int)CurrentMouseState.X, (int)CurrentMouseState.Y);
            MousePositionWorld = LevelEditor.Camera.ScreenToWorld(new Vector2(CurrentMouseState.X, CurrentMouseState.Y));

            //Make sure we only listen to mouse down clicks that are made on screen!
            if (Kristall.ScreenManager.Game.GraphicsDevice.Viewport.Bounds.Contains(mouseScreenPoint))
            {
                if (LastMouseState.LeftButton == ButtonState.Released && CurrentMouseState.LeftButton == ButtonState.Pressed)
                 //|| LastMouseState.RightButton == ButtonState.Released && CurrentMouseState.RightButton == ButtonState.Pressed)
              {
                  MouseDown(MousePositionWorld);
              }
            }

            if (LastMouseState.LeftButton == ButtonState.Pressed && CurrentMouseState.LeftButton == ButtonState.Released)
            {
                MouseUp(MousePositionWorld);
            }
        }

        public virtual void Update(float dt)
        {
        }

        public virtual void Draw(SpriteBatch batch)
        {
            batch.Draw(Cursor, MousePositionWorld - CursorOffset, Color.White);
            //batch.DrawString(Kristall.ScreenManager.Font, MousePosition.ToString(), -LevelEditor.Camera.Position, Color.White);
        }
    }
}
